<?php

namespace App\Console\Commands;

use App\Enum\LockHistoryEnum;
use App\Http\Library\Paas;
use App\Http\Model\Console\DwCronModel;
use App\Http\Model\Console\DwModel;
use Illuminate\Console\Command;

class DwCron extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'job:dw_cron';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '检查数仓取数任务执行结果';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        /*需要处理的业务线-分校数据库*/
        $list = DwModel::listDw();
        /*需要检查的任务记录*/
        $cron = self::cron($list);
        if (empty($cron)) {
            return 0;
        }
        $failure = [];
        $success = [];
        $notExist = [];
        foreach ($cron as $val) {
            /*查询当前任务执行情况*/
            $data = DwCronModel::data($val);
            /*没有执行的任务*/
            if (empty($data)) {
                $notExist[] = $val;
                continue;
            }
            /*执行成功的任务*/
            if ($data->status === LockHistoryEnum::STATUS_SUCCESS) {
                $success[] = $val;
            } else {
                /*失败的任务*/
                $failure[] = $val;
            }
        }
        log_arr([
            '[job:dw_cron]',
            'msg:检查数仓取数任务执行结果',
            'cron'      => $cron,
            'not_exist' => $notExist,
            'failure'   => $failure,
            'success'   => $success,
        ], 'pfs');

        $paas = ['[job:dw_cron]'];
        if (!empty($notExist)) {
            $paas['存在没有触发的任务'] = $notExist;
        }
        if (!empty($failure)) {
            $paas['存在执行失败的任务'] = $failure;
        }
        if (!empty($success)) {
            $paas['执行完成的任务'] = $success;
        }
        Paas::yach($paas);
        return 0;
    }


    /**
     * 需要检查的任务记录
     *
     * @param $list
     *
     * @return array
     */
    protected static function cron($list)
    {
        if (empty($list)) {
            return [];
        }
        $cron = [];
        foreach ($list as $val) {
            /*数仓-班级划拨时间表ClassTrans*/
            $cron[] = [
                'db'    => $val->db,
                'table' => LockHistoryEnum::DWD_FIN_CAPITAL_SUPERVISION_TRANSFER_LIST_FD,
                'date'  => date('Y-m-d'),
            ];
            /*数仓-当日推送缴退费汇总表PayClassList（按班级汇总）*/
            $cron[] = [
                'db'    => $val->db,
                'table' => LockHistoryEnum::DWS_FIN_CAPITAL_SUPERVISION_PAY_CLASS_LIST_RESULT_FD,
                'date'  => date('Y-m-d'),
            ];
            /*数仓-发生收退费行为的班级列表*/
            $cron[] = [
                'db'    => $val->db,
                'table' => LockHistoryEnum::DWD_FIN_CAPITAL_SUPERVISION_CLASS_LIST_ADD_FD,
                'date'  => date('Y-m-d'),
            ];
        }
        return $cron;
    }
}
